function recognizeType(jsonData) {
    let type = 'point';
    let geoCoordModel = (jsonData.data && jsonData.data[0].geoCoordModel);
    if (geoCoordModel && geoCoordModel.type == 'point') { //点数据
        type = 'point';
    } else if (geoCoordModel && geoCoordModel.type == 'radiation') { //辐射范围的数据。 原始数据中type值为point，为了方便程序识别，我手动修改了数据
        type = 'radiation';
    } else if (geoCoordModel && geoCoordModel.type == 'polygon') { //封闭的多边形
        type = 'polygon';
    }
    else if (jsonData.features && jsonData.type == 'FeatureCollection') { //线组合数据
        type = 'multiLine';
    } else if (jsonData.data && jsonData.data[0].luxianhuizhi) { //线数据
        type = 'line';
    }
    return type;
}

function getPoiIcon(dataUrl) {
    // 找到最后一个'/'字符的位置
    let lastFileSuffixIndex = dataUrl.lastIndexOf('/');
    // 截取从开始到这个位置的字符串
    let truncatedString = dataUrl.substring(0, lastFileSuffixIndex);

    let lastPointIndex = dataUrl.lastIndexOf('.');
    let simpleName = dataUrl.substring(lastFileSuffixIndex + 1, lastPointIndex);

    let poiIcon = truncatedString + '/' + simpleName + '.png';
    return poiIcon;
}

function parsePoint(dataUrl, jsonData, callback) {
    let poiIcon = getPoiIcon(dataUrl);

    let dataArray = jsonData.data;
    let markerArray = [];
    dataArray.forEach(element => {
        let poiCenter = JSON.parse(element.geoCoordModel.center);
        if (!poiCenter) {
            return;
        }
        let position = new GE.Point(poiCenter[0], poiCenter[1], poiCenter[2], '4326');
        let marker = addMarker(poiIcon, position, element);
        markerArray.push(marker);
    })

    if (callback) {
        callback(markerArray);
    }
}

function parsePolyline(dataUrl, jsonData, lineWidth, lineColor, callback) {
    let dataArray = jsonData.data;
    let lineArray = [];
    dataArray.forEach(function (element, index, array) {
        let pathData = JSON.parse(element.luxianhuizhi);
        let path = pathData.coords;
        let line = addLine(path, lineWidth, lineColor, element);
        lineArray.push(line);
    })

    if (callback) {
        callback(lineArray);
    }
}

function parsePolygon(dataUrl, jsonData, lineWidth, lineColor, callback) {
    let poiIcon = getPoiIcon(dataUrl);

    let dataArray = jsonData.data;
    let polygonArray = [];
    dataArray.forEach(element => {
        let path = element.geoCoordModel.coordinateArray;
        if (!path) {
            return;
        }
        let polygon = addPolygon(path, lineWidth, lineColor, poiIcon, element);

        let poiCenter = JSON.parse(element.geoCoordModel.center);
        if (!poiCenter) {
            return
        }
        let position = new GE.Point(poiCenter[0], poiCenter[1], poiCenter[2], '4326');
        let marker = addMarker(poiIcon, position, element);

        polygonArray.push({model: polygon, marker: marker});
    })

    if (callback) {
        callback(polygonArray);
    }
}

function addMarker(imageUrl, point, poiMsg) {
    //初始化要显示的标注的html内容
    const div = document.createElement('div');
    div.className = 'label';
    div.style.backgroundColor = 'transparent';

    const img = document.createElement('img');
    img.draggable = false; //针对火狐浏览器，解决鼠标粘连的问题
    img.style.webkitUserDrag = 'none'; //针对webkit，解决鼠标粘连的问题
    img.src = imageUrl;
    img.width = 40;
    img.height = 40;
    div.style.marginLeft = -img.width / 2 + 'px';
    div.style.marginTop = -img.height + 'px';
    div.appendChild(img);
    //初始化标注组件，并将htmlelement作为参数传入，这里是一个div
    let label = new GE.HtmlMarker(div);
    //设置组件在三维场景中的位置
    let position = point.changeEPSGType('3857'); //;
    label.position.set(position.x, position.y, 0);
    //设置对其方式，0,0为中心对齐
    label.center.set(0, 0);
    // label.openCollisionCheck(); //开启marker的碰撞检测
    //将组件添加至场景中
    app.add(label);

    label['poiMsg'] = poiMsg;
    return label;
}

function addLine(path, lineWidth, lineColor, lineMsg) {
    if (!path || path.length == 0) {
        return;
    }
    let pointArray = [];
    path.forEach(element => {
        let point = new GE.Point(element[0], element[1], element[2], '4326');
        pointArray.push(point);
    });

    let lineData = {
        pointArray: pointArray,  //多边形的顶点数组
        color: lineColor,
        lineWidth: lineWidth,  //线的宽度
    };
    let polyline = new GE.Polyline(lineData);
    app.add(polyline);

    return polyline;
}

function addPolygon(path, lineWidth, lineColor, imageUrl, polygonMsg) {
    if (!path || path.length == 0) {
        return;
    }
    let pointArray = [];
    path.forEach(element => {
        let point = new GE.Point(element[0], element[1], element[2], '4326');
        pointArray.push(point);
    });
    pointArray.push(pointArray[0].clone()); //闭合多边形

    let lineData = {
        pointArray: pointArray,  //多边形的顶点数组
        color: lineColor,
        lineWidth: lineWidth,  //线的宽度
    };
    let polyline = new GE.Polyline(lineData);
    app.add(polyline);
}

function parseMultiLine(dataUrl, jsonData, lineWidth, lineColor, callback) {
    let featureArray = jsonData.features;
    let lineArray = [];
    featureArray.forEach(function (feature, index, array) {
        let geometry = feature.geometry;
        let pathArray = geometry.coordinates;
        pathArray.forEach(path => {
            let line = addLine(path, lineWidth, lineColor, feature);
            lineArray.push(line);
        });

        // let pathData = JSON.parse(element.luxianhuizhi);
        // let path = pathData.coords;
        // let line = addLine(path, lineWidth, lineColor, element);
        // lineArray.push(line);
    })

    if (callback) {
        callback(lineArray);
    }
}

function parseRadiation(dataUrl, jsonData, radiationColor, radiationRadius, callback) {
    let poiIcon = getPoiIcon(dataUrl);

    let dataArray = jsonData.data;
    let radiationArray = [];
    dataArray.forEach(element => {
        let radiationCenter = JSON.parse(element.geoCoordModel.center);
        // let position = new GE.Point(radiationCenter[0], radiationCenter[1], radiationCenter[2], '4326');
        let model = addRadiation(radiationCenter, radiationColor, radiationRadius, element);

        let position = new GE.Point(radiationCenter[0], radiationCenter[1], radiationCenter[2], '4326');
        let marker = addMarker(poiIcon, position, element);

        radiationArray.push({model: model, marker: marker});
    })

    if (callback) {
        callback(radiationArray);
    }
}

function addRadiation(radiationCenter, radiationColor, radiationRadius, radiationMsg) {
    let RadarScan = new GE.RadarScan({
        center: [radiationCenter[0], radiationCenter[1], radiationCenter[2]],
        radius: radiationRadius,
        color: radiationColor,
        time: 3000
    });
    app.add(RadarScan);
}
